-- -- 免疫掉san光环
-- AddComponentPostInit("sanityaura", function(SanityAura)
--     local oldGetAura = SanityAura.GetAura
--     function SanityAura:GetAura(observer)
--         local val = oldGetAura(self, observer)
--         if observer.prefab == "twilight_archangel" then
--             return 0
--         end
--     end
-- end)
-- 破碎值低于25时无法搬运重物穿戴大理石甲
AddComponentPostInit("equippable", function(Equippable)
    local oldEquip = Equippable.Equip
    function Equippable:Equip(owner, from_ground)
        oldEquip(self, owner, from_ground)
        if owner ~= nil and owner.components.ta_fragmentate then
            if (self.inst:HasTag("heavy") or self.inst.prefab == "armormarble") and
                owner.components.ta_fragmentate.current < TUNING.TWILIGHT_ARCHANGEL_FRAGMENTATE * 0.25 then
                owner:DoTaskInTime(0, function()
                    if owner.components.inventory then
                        owner.components.inventory:DropItem(self.inst, true, true)
                    end
                end)
            end
        end
    end
end)

-- 破碎值低于25时攻击与开采矿物伐木的行为会扣除1点破碎值
AddComponentPostInit("workable", function(Workable)
    local oldWorkedBy_Internal = Workable.WorkedBy_Internal
    function Workable:WorkedBy_Internal(worker, numworks)
        print("worker=", worker, worker.components.ta_fragmentate, numworks, self.action)
        if worker ~= nil and worker.components.ta_fragmentate and worker.components.ta_fragmentate.current <
            TUNING.TWILIGHT_ARCHANGEL_FRAGMENTATE * 0.25 and
            (self.action == ACTIONS.CHOP or self.action == ACTIONS.MINE) then
            worker.components.ta_fragmentate:DoDelta(-1)
            oldWorkedBy_Internal(self, worker, 1000)
        else
            oldWorkedBy_Internal(self, worker, numworks)
        end
    end
end)

-- 让不老表在回血的同时回复4点破碎值
AddPrefabPostInit("pocketwatch_heal", function(inst)
    if inst.components.pocketwatch then
        local oldDoCastSpell = inst.components.pocketwatch.DoCastSpell
        inst.components.pocketwatch.DoCastSpell = function(inst, doer, target, pos)
            if doer.components.ta_fragmentate then
                if doer.pocketwatchHealCount == nil then
                    doer.pocketwatchHealCount = 1
                else
                    doer.pocketwatchHealCount = doer.pocketwatchHealCount + 1
                end
                if doer.pocketwatchHealCount >= 5 then
                    -- doer.components.ta_fragmentate:DoDelta(TUNING.TWILIGHT_ARCHANGEL_FRAGMENTATE_HEAL)
                    doer.components.ta_fragmentate:DoDelta(1)
                    doer.pocketwatchHealCount = 0
                end

            end
            return oldDoCastSpell(inst, doer, target, pos)
        end
    end
end)

do
    -- 替换克雷的only used by 的话
    local charactername = {"wanda"}
    local function TraceWithStack(character, root, tab, path)
        local stack = {{tab, path}}
        while #stack > 0 do
            local curtbl, curpath = unpack(table.remove(stack))
            for k, v in pairs(curtbl) do
                if type(v) == "table" then
                    table.insert(curpath, k)
                    table.insert(stack, {v, {unpack(curpath)}})
                    table.remove(curpath)
                else
                    for k2, v2 in pairs(root) do
                        if k2 ~= curpath[1] then
                            local data = v2
                            for i = 2, #curpath do
                                data = data[curpath[i]]
                                if not data then
                                    break
                                end
                            end
                            if type(data) == "table" then
                                if data[k] == "only_used_by_" .. string.lower(character) then
                                    data[k] = v
                                end
                            end
                        end
                    end
                end
            end
        end
    end
    for _, p in ipairs(charactername) do
        local character = string.upper(p)
        local tab = STRINGS
        local stack = {{tab}}
        while #stack > 0 do
            local curtbl = table.remove(stack)
            for k, v in pairs(curtbl) do
                if type(v) == "table" then
                    if v[character] and type(v[character]) == "table" then
                        TraceWithStack(character, v, v[character], {character})
                    else
                        table.insert(stack, v)
                    end
                end
            end
        end
    end
end
-- 旺达的部分能力
local function onbecamehuman(inst, data, isloading)
    if inst.components.positionalwarp ~= nil then
        if not isloading then
            inst.components.positionalwarp:Reset()
        end
        if inst.components.inventory:HasItemWithTag("pocketwatch_warp", 1) then
            inst.components.positionalwarp:EnableMarker(true)
        end
    end
end

local function onbecameghost(inst, data)
    if inst.components.positionalwarp ~= nil then
        inst.components.positionalwarp:EnableMarker(false)
    end
end
local function on_show_warp_marker(inst)
    inst.components.positionalwarp:EnableMarker(true)
end

local function on_hide_warp_marker(inst)
    inst.components.positionalwarp:EnableMarker(false)
end
local function OnWarpBack(inst, data)
    if inst.components.positionalwarp ~= nil then
        if data ~= nil and data.reset_warp then
        else
            inst.components.positionalwarp:GetHistoryPosition(true)
        end
    end
end
local wanda_main = function(inst)
    inst:AddTag("pocketwatchcaster")
    inst:AddTag("clockmaker")
    if not TheWorld.ismastersim then
        return
    end
    inst:AddComponent("oldager")
    inst.components.oldager:AddValidHealingCause("pocketwatch_heal")
    inst.components.oldager:AddValidHealingCause("debug_key")
    inst.components.oldager.base_rate = 0

    inst:AddComponent("positionalwarp")
    inst:DoTaskInTime(0, function()
        inst.components.positionalwarp:SetMarker("pocketwatch_warp_marker")
    end)
    inst.components.positionalwarp:SetWarpBackDist(TUNING.WANDA_WARP_DIST_NORMAL)

    inst:ListenForEvent("show_warp_marker", on_show_warp_marker)
    inst:ListenForEvent("hide_warp_marker", on_hide_warp_marker)

    inst:ListenForEvent("onwarpback", OnWarpBack)

    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
    inst:ListenForEvent("ms_becameghost", onbecameghost)
    if inst:HasTag("playerghost") then
        onbecameghost(inst)
    elseif inst:HasTag("corpse") then
        onbecameghost(inst, {
            corpse = true
        })
    else
        onbecamehuman(inst, nil, true)
    end
end
AddPrefabPostInit("twilight_archangel", wanda_main)

-- 易伤处理
AddPlayerPostInit(function(inst)
    if inst.components.health then
        local oldHealthDoDelta = inst.components.health.DoDelta
        inst.components.health.DoDelta = function(...)
            -- 如果有易伤标签，则ta所受所有伤害都加深
            if inst:HasTag("susceptible") then
                local oldclamp = math.clamp
                math.clamp = function(v, min, max, ...)
                    if v > 1 and min == 0 and max == 1 then
                        max = 999
                    end
                    return oldclamp(v, min, max, ...)
                end
                oldHealthDoDelta(...)
                math.clamp = oldclamp
            else
                oldHealthDoDelta(...)
            end
        end
    end
end)

-- 隐藏装备
AddComponentPostInit("equippable", function(Equippable)
    local oldEquip = Equippable.Equip
    function Equippable:Equip(owner)
        oldEquip(self, owner)
        if owner.prefab == "twilight_archangel" and (self.equipslot ~= EQUIPSLOTS.HANDS) then
            owner.AnimState:ClearOverrideSymbol("swap_body")
            -- owner.AnimState:ClearOverrideSymbol("swap_object")
            owner.AnimState:ClearOverrideSymbol("swap_body_backback")
            -- owner.AnimState:ClearOverrideSymbol("swap_body")
            owner.AnimState:ClearOverrideSymbol("swap_hat") -- 帽子
        end
        -- if owner.prefab == "camellia" and (self.equipslot == EQUIPSLOTS.BODY or -- 身体
        -- -- self.equipslot==EQUIPSLOTS.HANDS or --手
        -- self.equipslot == EQUIPSLOTS.BACK or -- 背包
        -- self.equipslot == EQUIPSLOTS.NECK or -- 护符
        -- self.equipslot == EQUIPSLOTS.HEAD -- 头部
        -- ) then
        --     owner.AnimState:ClearOverrideSymbol("swap_body")
        --     -- owner.AnimState:ClearOverrideSymbol("swap_object")
        --     owner.AnimState:ClearOverrideSymbol("swap_body_backback")
        --     -- owner.AnimState:ClearOverrideSymbol("swap_body")
        --     owner.AnimState:ClearOverrideSymbol("swap_hat") -- 帽子
        -- end
    end
end)
AddComponentPostInit("talker", function(Talker)
    local oldSay = Talker.Say
    function Talker:Say(script, ...)
        print(script, ...)
        if self.inst.prefab == "twilight_archangel" and script == "only_used_by_wanda" then
            return
        else
            oldSay(self, script, ...)
        end
    end
end)

